package tritop.android.a4dinfo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class StatsShow extends Activity {

	//Callback Interface for Visualizer View
	public interface ViewGraphSelected{
		void pointSelected(int index);
	}
	
	private static final String LOGTAG="STATSSHOW";
	private String[] mStatsAvail;
	private DBHelper dbhelper;
	private Spinner mStatsSpinner;
	private ImageView mCountryImage;
	private int mCCode;
	private Visualizer visView;
	private List<String[]> values;
	private TextView mTextTop,mTextBottom;
	private String[] valueDescriptions;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.visactivitylayout);
        values= new ArrayList<String[]>();
        valueDescriptions= new String[2];
        this.mStatsSpinner = (Spinner) this.findViewById(R.id.spinnerAvailableStats);
        this.mCountryImage = (ImageView) this.findViewById(R.id.imageViewBackground);
        this.visView = (Visualizer) this.findViewById(R.id.visualizer1);
        this.mTextTop= (TextView) this.findViewById(R.id.textViewTop);
        this.mTextBottom= (TextView) this.findViewById(R.id.textViewBottom);
        Bundle extras = getIntent().getExtras();
        this.mCCode = extras.getInt(A4DInfo.INTENT_COUNTRYCODE);
        setCountryBackground(mCCode);
	}

	
	/**************************************************************************
	 * close db connection
	 **************************************************************************/
	
	@Override
	protected void onPause() {
		this.dbhelper.close();
		super.onPause();
	}

	
	/**************************************************************************
	 * open db connection and setup the data adapter for the spinner widget
	 **************************************************************************/
	
	@Override
	protected void onResume() {
		super.onResume();
		this.dbhelper= new DBHelper(this);
		this.mStatsAvail=dbhelper.getAvailableStats(2);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mStatsAvail);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		this.mStatsSpinner.setAdapter(adapter);
		setupListener();
	}

	
	/**************************************************************************
	 * switch the country background image to the correct state
	 * countryid comes in the startAxctivity intent extraData
	 **************************************************************************/
	
	private void setCountryBackground(int countryid){
		switch(countryid){
			case A4DInfo.COUNTRYCODE_BW: mCountryImage.setImageResource(R.drawable.small_blue_badenwuert_freisteller);break;
			case A4DInfo.COUNTRYCODE_BY: mCountryImage.setImageResource(R.drawable.small_blue_bayern_freisteller);break;
			case A4DInfo.COUNTRYCODE_BE: mCountryImage.setImageResource(R.drawable.small_blue_berlin_freisteller);break;
			case A4DInfo.COUNTRYCODE_BB: mCountryImage.setImageResource(R.drawable.small_blue_brandenburg_freisteller);break;
			case A4DInfo.COUNTRYCODE_HB: mCountryImage.setImageResource(R.drawable.small_blue_bremen_freisteller);break;
			case A4DInfo.COUNTRYCODE_HH: mCountryImage.setImageResource(R.drawable.small_blue_hamburg_freisteller);break;
			case A4DInfo.COUNTRYCODE_HE: mCountryImage.setImageResource(R.drawable.small_blue_hessen_freisteller);break;
			case A4DInfo.COUNTRYCODE_MV: mCountryImage.setImageResource(R.drawable.small_blue_meckpom_freisteller);break;
			case A4DInfo.COUNTRYCODE_NI: mCountryImage.setImageResource(R.drawable.small_blue_niedersachsen_freisteller);break;
			case A4DInfo.COUNTRYCODE_NW: mCountryImage.setImageResource(R.drawable.small_blue_nordrheinwest_freisteller);break;
			case A4DInfo.COUNTRYCODE_RP: mCountryImage.setImageResource(R.drawable.small_blue_rheinlandpfalz_freisteller);break;
			case A4DInfo.COUNTRYCODE_SL: mCountryImage.setImageResource(R.drawable.small_blue_saarland_freisteller);break;
			case A4DInfo.COUNTRYCODE_SN: mCountryImage.setImageResource(R.drawable.small_blue_sachsen_freisteller);break;
			case A4DInfo.COUNTRYCODE_ST: mCountryImage.setImageResource(R.drawable.small_blue_sachsenanhalt_freisteller);break;
			case A4DInfo.COUNTRYCODE_SH: mCountryImage.setImageResource(R.drawable.small_blue_schleswigholstein_freisteller);break;
			case A4DInfo.COUNTRYCODE_TH: mCountryImage.setImageResource(R.drawable.small_blue_thueringen_freisteller);break;
		}
	}
	
	
	/**************************************************************************
	 * Gets called from the sinner widget item selected listener
	 * retrieves new data for the graph from database
	 **************************************************************************/
	
	private void spinnerSwitchedTo(String desc){
		int id=this.dbhelper.getIdbyDescription(desc);
		if(id>0){
			values=this.dbhelper.getEntries(id, this.mCCode);
			valueDescriptions=this.dbhelper.getValueDescriptions(id);
			this.visView.addData(values, mCCode);
			this.visView.invalidate();
		}
	}
	
	
	
	private void setupListener(){
		
		/**********************************************************************
		 * Item was selected in the spinner widget
		 **********************************************************************/
		
		this.mStatsSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				spinnerSwitchedTo((String)parent.getItemAtPosition(position));
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
				
			}
			
		});
		
		
		/**********************************************************************
		* Callback from Visualizer View get called when a point was selected
		* when this is called new data needs to be shown in the text views
		***********************************************************************/
		
		this.visView.setGraphSelectedListener(new ViewGraphSelected(){

			@Override
			public void pointSelected(int index) {
				mTextTop.setText(values.get(index)[0]+" "+valueDescriptions[0]); 
		        mTextBottom.setText(values.get(index)[1]+" "+valueDescriptions[1]);
			}
			
		});
	}
	
	
}
